package com.nec.recommend.utils;

import cn.hutool.core.codec.Base64Encoder;
import com.nec.recommend.auth.AuthUser;
import com.nec.recommend.consts.CacheGroupConst;
import com.nec.recommend.consts.TokenConst;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Optional;

/**
 * 认证用户工具类
 *
 * @author limingzhen
 * @date 2024/4/7
 */
public final class AuthUserUtils {

    private AuthUserUtils() {
    }

    private static final ThreadLocal<AuthUser> THREAD_LOCAL = new ThreadLocal<>();

    private static final Long DEFAULT_ID = -1L;

    public static void setUser(AuthUser user) {
        THREAD_LOCAL.set(user);
    }

    public static void removeUser() {
        THREAD_LOCAL.remove();
    }

    public static AuthUser getUser() {
        return Optional.ofNullable(THREAD_LOCAL.get()).orElse(new AuthUser());
    }

    /**
     * 获取用户id
     */
    public static Long getUserId() {
        AuthUser user = getUser();
        return Optional.ofNullable(user.getId()).orElse(DEFAULT_ID);
    }

    /**
     * 刷新会话
     *
     * @param redisTemplate redis模板
     */
    public static void refreshSessionTime(String client, RedisTemplate<String, Object> redisTemplate) {
        AuthUser user = getUser();
        redisTemplate.expire(TokenConst.MANAGE.equals(client) ? (CacheGroupConst.MANAGE_USER_SESSION + user.getToken()) : CacheGroupConst.WEAPP_USER_SESSION + user.getToken(), TokenConst.TOKEN_EXPIRE, TokenConst.EXPIRE_UNIT);
    }

    /**
     * 对密码进行加密
     *
     * @return 加密后的密码
     */
    public static String encodePwd(String pwd, String salt) {
        return Base64Encoder.encode(Md5Crypt.apr1Crypt(pwd.getBytes(StandardCharsets.UTF_8), TokenConst.TOKEN_PREFIX + salt).getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 匹配密码
     *
     * @param pwdRaw 数据库中用户的密码
     * @param pwd    用户输入的密码
     * @return 密码比对结果
     */
    public static Boolean matchPwd(String pwdRaw, String pwd, String account) {
        return StringUtils.equals(pwdRaw, encodePwd(pwd, account));
    }

    public static void main(String[] args) {
        String account = "17607977842";
        String pwd = "123456";
        String encodePwd = encodePwd(pwd, account);
        String encodePwd2 = encodePwd(pwd, account);
        System.out.println("加密后的密码：" + encodePwd);
        System.out.println("加密后的密码2：" + encodePwd2);
        System.out.println("密码匹配结果：" + matchPwd(encodePwd, pwd, account));
        //        加密后的密码：$1$sYDaqNzh$w.Pd.6b.8rEdfG2LHQkRk1
        //        密码匹配结果：false
    }

    /**
     * 姓名脱敏
     *
     * @param realname 姓名
     * @return 脱敏后的姓名
     */
    public static String desen(String realname) {
        int length = realname.length();
        if (length >= 3) {
            String str = realname.substring(1, realname.length() - 1);
            return realname.charAt(0) + StringUtils.rightPad("", str.length(), "*") + realname.substring(realname.length() - 1);
        } else if (length == 2) {
            return realname.charAt(0) + "*";
        }
        return realname;
    }
}
